// 
//  XWinWrapper.cs
//  
//  Author:
//       Nikita Tsukanov <keks9n@gmail.com>
// 
//  Copyright (c) 2011 Nikita Tsukanov
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 



using System;
namespace SkypeTab
{
	
	public partial class XWinWrapper : Gtk.DrawingArea
	{

		private ManagedWindow m_Window;
		public ManagedWindow Window
		{
			get
			{
				return m_Window;
			}
		}
		public void Reset()
		{
			m_Window=null;
		}
		public XWinWrapper ()
		{
			HeightRequest=300;
			WidthRequest=500;
		}
		public void WrapWindow(ManagedWindow window)
		{
			Realize();
			//IntPtr xid=gdk_x11_drawable_get_xid(GdkWindow.Handle);
			window.Wrapper.Hide();
			X11.X.Flush();
			System.Threading.Thread.Sleep(200);
			window.Reparent(GdkWindow);
			//X11.X.XReparentWindow(X11.X.Display, window.Handle,xid,0,0);
			X11.X.Flush();
			System.Threading.Thread.Sleep(200);
			window.Wrapper.Show();
			X11.X.Flush();
			window.Raise();
			X11.X.Flush();
			m_Window=window;
			
			
		}
		protected override bool OnFocusInEvent (Gdk.EventFocus evnt)
		{
			m_Window.SetFocus();
			return true;
			
		}
		public void SetFocus()
		{
			if(m_Window!=null)
				m_Window.SetFocus();
		}
		public void Sync()
		{
			if(m_Window!=null)
				m_Window.MoveAndResize(Allocation.Left,Allocation.Top, Allocation.Width, Allocation.Height);	
		}
		
		protected override void OnSizeAllocated (Gdk.Rectangle allocation)
		{
			if(m_Window!=null)
			{
				
				m_Window.MoveAndResize(Allocation.Left,Allocation.Top,  allocation.Width, allocation.Height);
			}
			base.OnSizeAllocated (allocation);
		}
	}
	
}

